package tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import tingshu.util.DBUtil;
import tingshu.util.Log;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/studio/album/list.json")
public class ListJsonServlet extends HttpServlet {
    public static class ResultView {
        public UserVO currentUser;
        public List<AlbumView> albumList = new ArrayList<>();
    }
    public static class AlbumView {
        public int aid;
        public String title;
        public String cover;
        public String state;

        public AlbumView(int aid, String title, String cover, int state) {
            this.aid = aid;
            this.title = title;
            this.cover = cover;
            switch (state) {
                case 0:
                    this.state = "已下线";break;
                case 1:
                    this.state = "未发布";break;
                case 2:
                    this.state = "已发布";break;
                default:
                    this.state = "未知";break;
            }
        }
    }
    @Override
    @SneakyThrows
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession(false);
        UserVO currentUser = null;

        if (session != null) {
            currentUser = (UserVO) session.getAttribute("currentUser");
        }
        ResultView resultView = new ResultView();
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        ObjectMapper objectMapper = new ObjectMapper();

        if (currentUser == null) {
            Log.println("用户未登录");
        } else {
            resultView.currentUser = currentUser;

            try (Connection c = DBUtil.connection()) {
                String sql = "select aid, title, cover, state from albums where uid = ? order by aid desc";
                try (PreparedStatement ps = c.prepareStatement(sql)) {
                    ps.setInt(1, currentUser.uid);
                    Log.println("执行 SQL" + ps);
                    try (ResultSet rs = ps.executeQuery()) {
                        while (rs.next()) {
                            resultView.albumList.add(new AlbumView(
                                    rs.getInt("aid"),
                                    rs.getString("title"),
                                    rs.getString("cover"),
                                    rs.getInt("state")
                            ));
                        }
                    }
                }
            }
        }
        String json = objectMapper.writeValueAsString(resultView);
        writer.println(json);
    }
}
